/******************************************************************************
 QuoteScraperService extracts stock quotes from finance.google.com
    Copyright (C) 2011  Benjamin A. Gura

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

******************************************************************************/
#include "XMLextractor.hpp"
std::string XMLextractor::GetNodeAttribute(const std::string nodeName,
        const std::string attribute)
{
    xmlChar* node = GetNodeAttributeRecursive(nodeName,attribute,m_root_element);
    std::string result;

    if(!node)
        result.append("null");
    else
    {
        result.append(reinterpret_cast<const char*>(node));
        xmlFree(node);
    }
    
    return(result);
}

xmlChar* XMLextractor::GetNodeAttributeRecursive(
    const std::string nodeName, const std::string attribute, xmlNode *node)
{
    xmlChar* result;
    xmlNode* cur = node;

    if(node->children != NULL)
        result = GetNodeAttributeRecursive(nodeName,attribute,node->children);

    if(node->next != NULL)
        result = GetNodeAttributeRecursive(nodeName,attribute,node->next);

    if(( !xmlStrcmp(cur->name, (const xmlChar *)nodeName.c_str() )))
        return( xmlGetProp(cur,(const xmlChar *)attribute.c_str() ) );
    else
        return(result);
}

std::string XMLextractor::GetNodeValue(const std::string nodeName)
{
    xmlChar* node = GetNodeValueRecursive(nodeName,m_root_element);
    std::string result;

    if(!node)
        result.append("null");
    else
    {
        result.append(reinterpret_cast<const char*>(node));
        xmlFree(node);
    }

    return(result);
}

xmlChar* XMLextractor::GetNodeValueRecursive(const std::string nodeName,
        xmlNode* node)
{
    xmlChar* result;
    xmlNode* cur = node;

    if(node->children != NULL)
        result = GetNodeValueRecursive(nodeName,node->children);
    
    if(node->next != NULL)
        result = GetNodeValueRecursive(nodeName,node->next);

    if(( !xmlStrcmp(cur->name, (const xmlChar *)nodeName.c_str() )))
        return( xmlNodeListGetString(m_xmlDoc,cur->xmlChildrenNode, 1) );

    else
        return(result);
}
void XMLextractor::LoadXMLConfigurationFile(const std::string fileName)
{
    m_xmlDoc = xmlReadFile(fileName.c_str(), NULL, 0);

    if (m_xmlDoc == NULL)
    {
        fprintf(stderr, "Failed to parse %s\n",
                fileName.c_str());
        exit(1);
    }
    else
    {
        m_root_element =
                xmlDocGetRootElement(m_xmlDoc);
        std::cerr << "Loaded xml configuration file.\n";
    }
}
void XMLextractor::LoadXMLFromMemory(std::string xml)
{
    m_xmlDoc = xmlReadMemory(xml.c_str(), xml.length(), "noname.xml", NULL, 0);

    if (m_xmlDoc == NULL)
    {
        fprintf(stderr, "In memory xml file\n");
        exit(1);
    }
    else
    {
        m_root_element = xmlDocGetRootElement(m_xmlDoc);
        std::cerr << "Loaded xml from memory\n";
    }
}
XMLextractor::~XMLextractor()
{
    xmlFreeDoc(this->m_xmlDoc);
    //xmlFree(this->m_root_element);
    xmlCleanupParser();
}